package com.mingqijia.gassafety.job.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mingqijia.gassafety.authority.dal.helper.ConfigureHolder;
import com.mingqijia.gassafety.db.entity.Consumer;
import com.mingqijia.gassafety.db.entity.RunningRecord;
import com.mingqijia.gassafety.db.mapper.RunningRecordMapper;
import com.mingqijia.gassafety.job.request.WarningRecordInfoRequest;
import com.mingqijia.gassafety.job.response.RecordDetailListResponse;
import com.mingqijia.gassafety.job.service.ConsumerService;
import com.mingqijia.gassafety.job.service.EquipmentService;
import com.mingqijia.gassafety.job.service.RunningRecordService;
import com.mingqijia.gassafety.shared.constant.Constants;
import com.mingqijia.gassafety.shared.utils.ApplicationUtils;
import com.mingqijia.gassafety.shared.utils.commonUtils.PageUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 设备运行报告的通知记录 服务实现类
 * </p>
 *
 * @author shiying.li
 * @since 2023-12-22
 */
@Service
public class RunningRecordServiceImpl extends ServiceImpl<RunningRecordMapper, RunningRecord> implements RunningRecordService {

    @Autowired
    ConsumerService consumerService;

    @Autowired
    ConfigureHolder holder;



    @Override
    public List<RecordDetailListResponse> getDetailList(WarningRecordInfoRequest request) {
        String spId = ApplicationUtils.getWorkingSpId();
        List<Integer> consumerIds =  null;
        if (request.getConsumerInfo() != null){
            LambdaQueryWrapper<Consumer> consumerLqw = new LambdaQueryWrapper<>();
            consumerLqw.eq(Consumer::getIsDeleted, Constants.IS_DELETED_FALSE)
                    .eq(Consumer::getSpId, spId)
                    .and(lqw -> lqw.eq(Consumer::getName, request.getConsumerInfo())
                            .or().eq(Consumer::getSubsCode,request.getConsumerInfo()));
            List<Consumer> consumerList = consumerService.list(consumerLqw);
            consumerIds = consumerList.stream().map(Consumer::getId).collect(Collectors.toList());
        }
        LambdaQueryWrapper<RunningRecord> lqw = new LambdaQueryWrapper<>();
        lqw.eq(RunningRecord::getTaskIdentificationCode, request.getTaskIdentificationCode())
                .eq(RunningRecord::getIsDeleted, Constants.IS_DELETED_FALSE)
                .eq(!StringUtils.isEmpty(request.getTelephone()), RunningRecord::getPushPhone,request.getTelephone())
                .eq(request.getSendResult() != null, RunningRecord::getSendResult,request.getSendResult())
                .eq(request.getSendType() != null, RunningRecord::getSendType,request.getSendType())
                .eq(RunningRecord::getSpId, spId).in(!CollectionUtils.isEmpty(consumerIds), RunningRecord::getConsumerId,consumerIds);
        Page<RunningRecord> page = PageHelper.startPage(request.getPage(), request.getPageSize());
        List<RunningRecord> list = this.list(lqw);
        List<RecordDetailListResponse> collect = list.stream().map(item -> {
            RecordDetailListResponse response = new RecordDetailListResponse();
            BeanUtils.copyProperties(item, response);
            if (!StringUtils.isEmpty(item.getObjLevel())) {
                response.setObjLevel("级别" + item.getObjLevel());
            }
            Consumer consumer = consumerService.getById(item.getConsumerId());
            String consumerInfo = StringUtils.isEmpty(consumer.getCustName())
                    ? consumer.getSubsCode()
                    : StringUtils.isEmpty(consumer.getSubsCode())
                    ? consumer.getCustName()
                    : consumer.getCustName() + "(" + consumer.getSubsCode() + ")";
            response.setConsumerInfo(consumerInfo);
            response.setSendResult(item.getSendResult() == 0 ? "发送成功":item.getSendResult() == 1? "发送失败":"呼叫中（外呼）");
            response.setSendType(item.getSendType() == 1 ? "短信":item.getSendType() == 2? "微信":"外呼");
            return response;
        }).collect(Collectors.toList());
        return collect;
    }
}
